<?php
!defined('P_W') && exit('Forbidden');

/**
 *
 * 出租房源服务类
 * @author hu.liaoh
 *
 */
require_once(R_P."mode/house/lib/service/secondbaseservice.php");
class PW_SecondHireService extends PW_SecondBaseService{

	var $hireType = array();//出租方式
	var $roomtype = array(); //户型
	var $_orderTypes = array();//排序类型
	var $_secondType = 'hire';

	function PW_SecondHireService(){
		$this->hireType = array('1'=>'整租', '2'=>'合租');
		$this->roomType = array('1'=>'一室 ', '2'=>'二室 ', '3'=>'三室 ', '4'=>'四室', '5'=>'五室 ', '6'=>'五室以上');
		$this->_orderTypes = array('modifytime', 'posttime', 'price');
	}

	/**
	 *
	 * 获取一条房源信息
	 * @param $houseid
	 */
	function getHireInfo($houseid){
		$infoDB = $this->_getInfoDB();		//从DB中取出数据
		$hireInfo = $infoDB->get($houseid);
		return $this->adapterSecondInfo($hireInfo, 'out');		//返回适配后的数据
	}

	/**
	 *
	 * 增加一条房源信息
	 * @param $hireModule
	 */
	function addHire($hireModule){
		if(!$this->_checkHireModule($hireModule)) return false;
		$hireInfo = $this->_moduleToArray($hireModule);
		if(!S::isArray($hireInfo)) return false;
		$data = $this->adapterSecondInfo($hireInfo,'in');	//对输入数据进行适配
		$houseid = $this->_addSecondInfo($data);	//增加详细信息表中的数据
		if(!$houseid) return false;
		$data['houseid'] = $houseid;	//把houseid添加到数据源
		$this->_addImage($houseid, $data, 'hire'); //增加图片信息表
		$this->_addExpired($houseid);
		if(!checkBroker($data['uid'])) $this->_addPersonal($data, 'hire');
		$this->_addPosttime($data);		//增加发布时间索引表中的数据
		$this->_addModifyTime($data);		//增加修改时间索引表中的数据
		$this->_addPrice($data);		//增加总售价索引表中的数据
		$this->_addSecondNum($data['uid'],1);
		return $houseid;
	}

	/**
	 *
	 * 获取经纪人最新更新的房源
	 * @param $uid
	 * @param $houseid
	 * @param $limit
	 */
	function getBorkerHiresByTime($uid, $houseid, $limit){
		$infoDB = $this->_getInfoDB();
		$result = $infoDB->getBorkerHires($uid, $houseid, $limit);
		return $this->_getDetailInfo($result);
	}
	
	/**
	 * 
	 * 更新一条房源信息
	 * @param $hireModule
	 * @param $houseids
	 */
	function updateHire($hireModule,$houseids){
		if(!$this->_checkHireModule($hireModule)) return false;
		$hireInfo = $this->_moduleToArray($hireModule);
		if(!S::isArray($hireInfo)) return false;
		$hireInfo = $this->adapterSecondInfo($hireInfo,'in');		//对输入数据进行适配过滤
		$this->_updateImage($houseids, $hireInfo); //增加图片信息表
		$this->_updateSecondInfo($hireInfo, $houseids);		//更新详细信息表中的数据
		if($hireInfo['state']) return $this->_updateOverdue($hireInfo, $houseids);
		if(!checkBroker($hireInfo['uid'])) $this->_updatePersonal($hireInfo, $houseids);
		$this->_updateModityTime($hireInfo, $houseids);		//更新修改时间索引表中的数据
		$this->_updatePostTime($hireInfo, $houseids);		//更新修改时间索引表中的数据
		$this->_updatePrice($hireInfo, $houseids);		//更新总售价索引表中的数据
		return true;
	}
	
	/**
	 *
	 * 删除一条多条房源信息
	 * @param $houseids
	 */
	function delete($houseids){
		if(!S::isArray($houseids)) $houseids = array($houseids);
		$this->_updateSecondNums($houseids);
		$this->_deleteSecondInfo($houseids);	//删除详细信息表中的数据
		$this->_deleteModifyTime($houseids);//删除修改时间索引表中的数据
		$this->_deletePostTime($houseids);	//删除发布时间索引表中的数据
		$this->_deletePrice($houseids);	//删除总售价索引表中的数据
		$this->_deleteImage($houseids); //删除图片
		$this->_deleteExpired($houseids);
		$this->_deleteOverdue($houseids); //删除过期索引表中数据
		$this->_deletePersonal($houseids);
		return true;
	}
	
	/**
	 *
	 * 获获出售module
	 * @param $houseid
	 */
	function getHouseHireModule($houseid){
		if(!$houseid) return $this->_getHireModule();
		$hireModule = $this->_getHireModule();
		$infoDB = $this->_getInfoDB();
		$hireInfo = $infoDB->get($houseid);
		return $this->_arrayToModule($hireInfo, $hireModule, 'hire');
	}
	
	/**
	 *
	 * 获取数据map
	 */
	function _getSecondMap(){
		return array('fitmenttype'=>'fitment','areatype'=>'areatype', 'hiretype'=>'hiretype','housetype'=>'housetype','attachs'=>'attachs', 'tel'=>'tel', 'email'=>'email', 'phone'=>'phone', 'linkman'=>'linkman', 'subject'=>'subject' ,'uid'=>'uid' ,'username'=>'username' ,'ischarge'=>'ischarge' ,'istop'=>'istop' ,'isurgent'=>'isurgent' ,'isbroker'=>'isbroker' ,'acreage'=>'acreage' ,'currentfloor'=>'currentfloor' ,'totalfloor'=>'totalfloor' ,'price'=>'price' ,'room'=>'room' ,'village'=>'village' ,'platetype'=>'plate' ,'address'=>'address' ,'buildtime'=>'buildtime' ,'traffic'=>'traffic' ,'fitment'=>'fitment' ,'equipment'=>'equipment' ,'thumb'=>'thumb' ,'state'=>'state' ,'descrip'=>'descrip' ,'gate'=>'gate' ,'posttime'=>'posttime' ,'modifytime'=>'modifytime');
	}
	
	/**
	 *
	 * 增加经纪人发布信息数量
	 * @param $uid
	 * @param $num
	 */
	function _addSecondNum($uid,$num){
		$brokerService = $this->_getBrokerService();
		$brokerService->addHireNum($uid,$num);
	}
	
	/**
	 *
	 * 重新发布房源
	 * @param $houseid
	 */
	function _overdueToNomal($houseids){
		global $timestamp;
		$overdueDB = $this->_getOverdueDB();
		$result = $overdueDB->gets($houseids);
		foreach($result as $key=>$value){
			$value['posttime'] = $value['modifytime'] = $timestamp;				
			$this->_addModifyTime($value);
			$this->_addPostTime($value);
			$this->_addPrice($value);
		}
		$overdueDB->delete($houseids);
		return true;
	}
	
	/**
	 *
	 * 移动过期房源到过期房索引表中
	 * @param $houseids
	 */
	function _nomalToOverdue($houseids){
		$infoDB = $this->_getInfoDB();
		$overdueDB = $this->_getOverdueDB();
		$result = $infoDB->getIndexInfo($houseids);
		foreach ($result as $key=>$value){
			$data = $this->_filterField($value, $overdueDB->getStruct());
			$overdueDB->add($data);
		}
		$this->_deleteModifyTime($houseids);
		$this->_deletePostTime($houseids);
		$this->_deletePrice($houseids);
		return true;
	}
	
	/**
	 *
	 * 更新过期信息索引表
	 * @param $hireInfo
	 * @param $houseids
	 */
	function _updateOverdue($hireInfo, $houseids){
		$overdueDB = $this->_getOverdueDB();
		$overdueData = $this->_filterField($hireInfo,$overdueDB->getStruct('update'));
		$retV = $overdueDB->update($overdueData, $houseids);
		if($retV == false) return false;
	}
	
	/**
	 *
	 * 对输入数据内容进行过安全过滤及枚举转换
	 * 需要过滤数据类型
	 * @param $hireInfo
	 */
	function _checkInput($hireInfo){
		global $timestamp;
		if($hireInfo['isbroker']){
			$borkerService = $this->_getBrokerService();
			$borker = $borkerService->getBroker($hireInfo['uid']);
			$hireInfo['linkman'] = $borker['realname'];
		}
		$hireInfo['datatype'] = 'hire';
		$hireInfo['posttime'] = $hireInfo['modifytime'] = $timestamp;
		$hireInfo['room'] = $hireInfo['room']['bedroom'] . $hireInfo['room']['hall'] . $hireInfo['room']['toilet'] . $hireInfo['room']['terrace'];
		$hireInfo['gate'] = $hireInfo['subject'].$hireInfo['village'];
		$hireInfo['roomtype'] = (int)$hireInfo['room']['bedroom'];	//计算房型
		if($hireInfo['roomtype'] > 5) $hireInfo['roomtype'] = 6;
		$hireInfo['pricetype'] = $this->_getIdFromRange('rentprice',$hireInfo['price']);	//计算售价区间
		return $hireInfo;
	}

	/**
	 *
	 * 对输出数据内容进行枚举转换
	 * @param $hireInfo
	 */
	function _checkOutput($hireInfo){
		$room = str_split($hireInfo['room']);
		$hireInfo['room'] = $room[0].'室'.$room[1].'厅'.$room[2].'卫'.$room[3].'阳台';	//户型转换
		$areaFields = $this->getFieldsByType('area');		//区域转换
		$hireInfo['areatype'] = $areaFields{$hireInfo['areatype']};
		$thumb = geturl($hireInfo['thumb']);
		$hireInfo['thumb'] = $thumb[0] ;
		if($hireInfo['fitment']){
			$fitmentFields = $this->getFieldsByType('paystatus');
			$hireInfo['fitment'] = $fitmentFields[$hireInfo['fitment']];
		}
		if(!is_file($hireInfo['thumb'])) $hireInfo['thumb'] = 'mode/house/images/sendhand/nopic_105x80.png';
		$hireInfo['neartime'] =  getLastDate($hireInfo['modifytime']);
		if(!$hireInfo['isbroker'])	$hireInfo['linkman'] = '个人 ';
		
		$pricetype = $this->_getIdFromRange('rentprice',$hireInfo['price']);
		($pricetype != $hireInfo['pricetype']) && $data['pricetype'] = $pricetype;
		if(is_array($data)){
			$this->_updateModityTime($data, $hireInfo['houseid']);		//更新修改时间索引表中的数据
			$this->_updatePostTime($data, $hireInfo['houseid']);		//更新修改时间索引表中的数据
			$this->_updatePrice($data, $hireInfo['houseid']);	
		}
		$topHire = $GLOBALS['house_hire'][$hireInfo['houseid']];
		$hireInfo['istop'] && $topHire && ($hireInfo['pricetype'] !=  $topHire['pricetype']) && $this->_updateConfig('hire');
		return $hireInfo;
	}

	/**
	 * 
	 * 检查module是否合法
	 * @param $hireModule
	 */
	function _checkHireModule($hireModule) {
		return is_object($hireModule) && get_class($hireModule) == 'PW_HouseHireModule';
	}
	
	/**
	 * 
	 * 获取过期索引表 db
	 */
	function _getOverdueDB(){
		return House::LoadDB('House_Hire_Overdue');
	}

	/**
	 *
	 * 获取服务工具类
	 * @param $name 工具类名称
	 */
	function _getSecondDB(){
		return House::LoadDB('House_SecondHire');
	}

	/**
	 *
	 * 获取个人信息服务
	 */
	function _getPersonalDB(){
		return House::LoadDB('House_Personal');
	}

	/**
	 *
	 * 获取出售房module
	 */
	function _getHireModule() {
		return House::loadModule('HouseHireModule');
	}
	/**
	 *
	 *获取信息表db类
	 */
	function _getInfoDB(){
		return House::LoadDB('House_Hire_Info');
	}

	/**
	 *
	 * 获取发布时间db类
	 */
	function _getPosttimeDB(){
		return House::LoadDB('House_Hire_Posttime');
	}

	/**
	 *
	 * 获取修改时间db类
	 */
	function _getModifytimeDB(){
		return House::LoadDB('House_Hire_Modifytime');
	}

	/**
	 *
	 * 获取售价db类
	 */
	function _getPriceDB(){
		return House::LoadDB('House_Hire_Price');
	}
}
